package com.yeyks.service.impl;

import com.yeyks.base.PageInfo;
import com.yeyks.dal.dao.DocInfoMapper;
import com.yeyks.service.DocInfoService;
import com.yeyks.base.PageInfo;
import com.yeyks.service.DocInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author fuchangshuai
 * @date 2019/3/1 10:56 AM
 */
@Slf4j
@Transactional
@Service
public class DocInfoServiceImpl implements DocInfoService {
    @Autowired
    private DocInfoMapper docInfoMapper;

    /**
     * 获取文档列表
     *
     * @param parent
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Map<String, Object> selectDocByPage(Integer parent, String keyword, Integer pageNum, Integer pageSize) {
        PageInfo pageInfo = PageInfo.of(pageNum, pageSize);
        List<Map> docInfoList;
        Integer count;
        //分类
        if (parent == 1) {
            docInfoList = docInfoMapper.selectByDirectorKeyword(true, keyword, pageInfo.getOffset(), pageInfo.getLimit());
            count = docInfoMapper.selectByDirectorKeywordCount(true, keyword);

        } else if (parent == 0) {//内容
            docInfoList = docInfoMapper.selectByDirectorKeyword(false, keyword, pageInfo.getOffset(), pageInfo.getLimit());
            count = docInfoMapper.selectByDirectorKeywordCount(false, keyword);
        } else {
            docInfoList = docInfoMapper.selectByParentKeyword(parent, keyword, pageInfo.getOffset(), pageInfo.getLimit());
            count = docInfoMapper.selectByParentKeywordCount(parent, keyword);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", docInfoList);
        map.put("count", count);
        return map;
    }
}
